let expr={};
window.vueInit=(fn)=>{};
let isLoad=/(http[s]:\/\/)|(\/\/)/;
let loadUrls={};
function get_url(url){
    if(chrome.extension){
        return isLoad.test(url)?url:chrome.extension.getURL(url);
    }
    return url;
}
function html_load(rex, callback) {
    let timer = setInterval(() => {
        if ($(rex).length > 0) {
            clearInterval(timer);
            callback && callback($(rex));
        }
    }, 10)
}
//同步
function async_html_js_eval(url,noEval=false) {
    if(loadUrls[url]){
        if(!noEval){
            try {
                (function(){
                    window.eval(loadUrls[url]);
                }).call(window);
            } catch (e) {
                console.error(e);
            }
        }
        return loadUrls[url];
    }
    let js = '';
    $.ajax({
        url: get_url(url),
        dataType: 'text',
        async: false,
        cache:false,
        success(par_js) {
            js = par_js;
            loadUrls[url]=par_js;
            if(!noEval){
                try {
                    (function(){
                        window.eval(par_js);
                    }).call(window);
                } catch (e) {
                    console.error(e);
                }
            }
        }
    });
    return js;
}
function async_html_get(url) {
    if(loadUrls[url]){
        return loadUrls[url];
    }
    let html = '';
    $.ajax({
        url:get_url(url),
        dataType: 'text',
        async: false,
        cache:false,
        success(ht) {
            html = ht;
            loadUrls[url]=html;
        }
    });
    return html;
}
//异步
function html_js_eval(url,noEval=false) {
    return new Promise((resolve, reject) => {
        if(loadUrls[url]){
            if(!noEval){
                try {
                    (function(){
                        window.eval(loadUrls[url]);
                    }).call(window);
                } catch (e) {
                    reject(e);
                }
            }
            return resolve(loadUrls[url]);
        }
        $.ajax({
            url: get_url(url),
            dataType: 'text',
            async: true,
            cache:false,
            success(par_js) {
                loadUrls[url]=par_js;
                if(!noEval){
                    try {
                        (function(){
                            window.eval(par_js);
                        }).call(window);
                        resolve(par_js);
                    } catch (e) {
                        reject(e);
                    }
                }else{
                    resolve(par_js);
                }
            }
        }).catch(reject);
    })
}
function html_get(url) {
    return new Promise((resolve, reject) => {
        if(loadUrls[url]){
            return resolve(loadUrls[url]);
        }
        $.ajax({
            url:get_url(url),
            dataType: 'text',
            async: true,
            cache:false,
            success(ht) {
                loadUrls[url]=ht;
                resolve(ht);
            }
        }).catch(reject);
    })
}

function css_load(url) {
    if(loadUrls[url]){
        return loadUrls[url];
    }
    loadUrls[url]=url;
    html_load('head', ($dom) => {
        $dom.append($('<link rel="stylesheet" href="' + get_url(url+'?t='+new Date().getTime()) + '">'))
    })
}
function loadVue(moduleUrl,global=true){
    expr.loading && expr.loading(true);
    let data= async_html_get(moduleUrl);
    if(data){
        //解析
        try{
            let html=$(data);
            html_load('head',()=>{
                $('head').append(html.siblings('style'));
            });
            let ret=null;
            function vueInit(vueTemplate){
                if(vueTemplate.data && typeof vueTemplate.data==='object'){
                    vueTemplate.el=html.siblings('template')[0];
                    return new Vue(vueTemplate);
                }
                vueTemplate.template=html.siblings('template').html();
                if(global){
                    Vue.component(vueTemplate.name,vueTemplate);
                }else{
                    ret=vueTemplate;
                }
                expr.loading && expr.loading(false);
            }
            eval(html.siblings('script').html());
            return ret;
        }catch (e) {
            console.log('ZY_ERR',e);
            expr.error && expr.error(moduleUrl);
            expr.loading && expr.loading(false);
        }
    }else{
        expr.error && expr.error(moduleUrl);
        expr.loading && expr.loading(false);
    }
}
//异步
expr.loadJs=html_js_eval;
expr.loadHtml=html_get;

//同步
expr.asyncLoadJs=async_html_js_eval;
expr.asyncLoadHtml=async_html_get;
expr.load_css=css_load;
expr.loadVue=loadVue;
expr.wait=function(rex){
    return new Promise((resolve, reject) => {
        try{
            html_load(rex,resolve)
        }catch (e) {
            reject(e);
        }
    })
};
module.exports=expr;
